package controllers
{
	import models.Diagram;
	import models.Project;
	import models.RESTResponse;
	import models.User;
	
	import mx.collections.HierarchicalData;
	import mx.core.Application;
	import mx.core.IFlexDisplayObject;
	import mx.managers.PopUpManager;
	
	import views.ContributorAddWindow;
	import views.DiagramEditWindow;
	import views.ProjectEditWindow;
	import views.UMLClassEditWindow;
	import views.UserEditWindow;
	
	public class WindowManager
	{
		private static var _current:String = null;
		private static var _main:String = null;
		
		public static const USER_LOGIN:String 			= 'user_login';
		public static const USER_REGISTER:String		= 'user_register';
		public static const USER_EDIT:String			= 'user_edit';
		public static const USER_FORGOT:String			= 'user_forgot';
		
		public static const PROJECT_LIST:String 		= 'project_list';
		public static const PROJECT_CREATE:String 		= 'project_create';
		public static const PROJECT_EDIT:String 		= 'project_edit';
		public static const PROJECT_DETAILS:String 		= 'project_details';
		public static const PROJECT_NO_DETAILS:String	= 'project_no_details';
		
		public static const DIAGRAM_CREATE:String		= 'diagram_create';
		public static const DIAGRAM_EDIT:String			= 'diagram_edit';
		public static const DIAGRAM_DETAILS:String		= 'diagram_details';
		
		public static const CLASS_CREATE:String			= 'class_create';
		
		public static const CONTRIBUTOR_ADD:String		= 'contributor_add';
		
		public function WindowManager() {
		}
		
		public static function get current() : String {
			return _current;
		}
		
		public static function get main() : String {
			return _main;
		}
		
		public static function open(id:String, args:Object = null) : void {
			var app:Object = Application.application;
			var popup:IFlexDisplayObject;
			
			if(_current != null) {
				close(_current);
				_current = null;
			}
			
			switch(id) {
				case USER_LOGIN:
					showModule('ui_modules_login');
				break;
				
				case USER_REGISTER:
					showModule('ui_modules_register');
				break;
				
				case USER_EDIT:
					popup = PopUpManager.createPopUp(app.self(), UserEditWindow, true);
					(popup as UserEditWindow).setUser(args as User);
					PopUpManager.centerPopUp(popup);
				break;
				
				case USER_FORGOT:
					showModule('ui_modules_forgot');
				break;
				
				/* Projects */ 
				
				case PROJECT_LIST:
					_main = PROJECT_LIST;					
					showModule('ui_modules_projects');
					
					var ptree:HierarchicalData = new HierarchicalData();
					ptree.source 			= app.projectManager.projects;
					ptree.childrenField 	= "diagrams";
					 
					app.ui_project_list.dataProvider = ptree;
					
					app.projectManager.updateList();
				break;
				
				case PROJECT_CREATE:
					app.projectManager.currentProject = null;
					popup = PopUpManager.createPopUp(app.self(), ProjectEditWindow, true);
					PopUpManager.centerPopUp(popup);
				break;
				
				case PROJECT_EDIT:
					if(args != null) {
						app.projectManager.currentProject = args as Project;
						popup = PopUpManager.createPopUp(app.self(), ProjectEditWindow, true);
						(popup as ProjectEditWindow).setProject(args as Project);
						PopUpManager.centerPopUp(popup);
					}
				break;
				
				case PROJECT_NO_DETAILS:					
					app.ui_project_details_help.visible = true;
					app.ui_project_details_help.includeInLayout = true;
					
					app.ui_project_details_project.visible = false;
					app.ui_project_details_project.includeInLayout = false;
					
					app.ui_project_details_diagram.visible = false;
					app.ui_project_details_diagram.includeInLayout = false;
				break;
				
				case PROJECT_DETAILS:
					app.projectManager.currentProject = args as Project;			
					app.ui_project_details_project_creator.text = (args as Project).owner.name;

					app.ui_project_details_help.visible = false;
					app.ui_project_details_help.includeInLayout = false;
					
					app.ui_project_details_project.visible = true;
					app.ui_project_details_project.includeInLayout = true;
					
					app.ui_project_details_diagram.visible = false;
					app.ui_project_details_diagram.includeInLayout = false;
					
					app.ui_project_details_project_name.text = (args as Project).name;
					app.ui_project_details_project_contributors.dataProvider = (args as Project).contributors;
				break;
				
				/* Diagrams */
				
				case DIAGRAM_CREATE:
					app.diagramManager.currentDiagram = null;
					popup = PopUpManager.createPopUp(app.self(), DiagramEditWindow, true);
					PopUpManager.centerPopUp(popup);
				break;
				
				case DIAGRAM_EDIT:
					_main = DIAGRAM_EDIT;
					showModule('ui_modules_design');
					var ctree:HierarchicalData = new HierarchicalData();
					ctree.source 			= app.umlClassManager.classes;
					ctree.childrenField		= "attributes";
					
					app.ui_design_classes.dataProvider = ctree;
					app.umlClassManager.updateList(app.projectManager.currentProject, function (response:RESTResponse) : void {
						app.diagramManager.drawDiagram();
					});
				break;
				
				case DIAGRAM_DETAILS:
					app.diagramManager.currentDiagram = args as Diagram;
					
					app.ui_project_details_help.visible = false;
					app.ui_project_details_help.includeInLayout = false;
					
					app.ui_project_details_project.visible = false;
					app.ui_project_details_project.includeInLayout = false;
					
					app.ui_project_details_diagram.visible = true;
					app.ui_project_details_diagram.includeInLayout = true;
					
					app.ui_project_details_diagram_name.text = (args as Diagram).name;
					app.ui_project_details_diagram_type.text = (args as Diagram).type;
					app.ui_project_details_diagram_parent_project.text = app.projectManager.getProjectById((args as Diagram).projectId).name;
				break;
				
				/* Classes */
				
				case CLASS_CREATE:
					app.umlClassManager.currentClass = null;
					popup = PopUpManager.createPopUp(app.self(), UMLClassEditWindow, true);
					PopUpManager.centerPopUp(popup);
				break;
				
				/* Contributors */
				
				case CONTRIBUTOR_ADD:
					popup = PopUpManager.createPopUp(app.self(), ContributorAddWindow, true);
					PopUpManager.centerPopUp(popup);
				break;
				
				default:
					UIErrorController.handleUIErrorCode("windowmanager_unknown");
			}
			
			_current = id;
		}
		
		public static function close(id:String) : void {
			_current = null;
		}
		
		private static function hideMainModules() : void {
			var app:Object = Application.application;
			
			app.ui_modules_login.includeInLayout		= false;
			app.ui_modules_login.visible 				= false;
			
			app.ui_modules_register.includeInLayout 	= false;
			app.ui_modules_register.visible 			= false;
			
			app.ui_modules_forgot.includeInLayout		= false;
			app.ui_modules_forgot.visible 				= false;
			
			app.ui_modules_projects.includeInLayout		= false;
			app.ui_modules_projects.visible 			= false;
			
			app.ui_modules_design.includeInLayout 		= false;
			app.ui_modules_design.visible 				= false;
			
		}
		
		private static function showModule(id:String) : void {
			var app:Object = Application.application;
			hideMainModules();
			app[id].includeInLayout = true;
			app[id].visible 		= true;
		}
	}
}